<?php
class ScoresController extends AppController {

var $name = 'Scores';

var $uses = array('Score','Application');

function beforeFilter() {
	parent::beforeFilter();
	if (isset($this->passedArgs[0]) && $this->Auth->user('group_id') == 2) {
  		$aco = $this->Acl->Aco->findByModelAndForeignKey('Score', $this->passedArgs[0]);
    	$aro = $this->Acl->Aro->findByModelAndForeignKey('User', $this->Auth->user('id'));
    	if (!$this->Acl->check($aro['Aro'], $aco['Aco'])) {
      	$this->Session->setFlash($this->Auth->authError, 'flash_failure');
        $this->redirect($this->referer());
    	}
		}
}

///////////////////////////////////////////////////////////////////////////////	

function edit($id = null) {
	
	if (!$id && empty($this->data)) {
		$this->Session->setFlash(__('Invalid Score', true), 'flash_failure');
		$this->redirect($this->referer());
	}
	
	if (!empty($this->data)) {
		if ($this->Score->saveAll($this->data)) {
			$this->Application->id = $this->Score->field('application_id');
			
			//We saved so change status.
			$this->Application->change_status($this->Application->id, 'gre', 2, '');
			if(isset($data['Application']['toefl_req']) && $data['Application']['toefl_req']) 
				$this->Application->change_status($this->Application->id, 'toefl', 2, '');
			$this->Session->setFlash(__('Scores have been updated', true), 'flash_success');
		} else {
			$this->Session->setFlash(
				__('Your Scores could not be saved. Please, try again.', true), 'flash_failure');
		}
	}
	if (empty($this->data)) {
		$this->data = $this->Score->read(null, $id);
	}	
	
	$this->set('app_id', $this->Score->field('application_id'));	
}

} //end controller.
